package redlib.backend.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import redlib.backend.dao.BillingDetailsMapper;
import redlib.backend.dto.BillingDetailsDTO;
import redlib.backend.dto.NameSelectDTO;
import redlib.backend.dto.TimeSelectDTO;
import redlib.backend.dto.query.BillingDetailsQueryDTO;
import redlib.backend.dto.query.NameQueryDTO;
import redlib.backend.dto.query.TimeQueryDTO;
import redlib.backend.model.*;
import redlib.backend.service.BillingDetailsService;
import redlib.backend.service.utils.BillingDetailsUtils;
import redlib.backend.utils.FormatUtils;
import redlib.backend.utils.PageUtils;
import redlib.backend.utils.ThreadContextHolder;
import redlib.backend.vo.BillingDetailsVO;
import redlib.backend.vo.NameSelectVO;
import redlib.backend.vo.TimeSelectVO;
import java.util.ArrayList;
import java.util.List;

@Service
public class BillingDetailsServiceImpl implements BillingDetailsService {
    @Autowired(required = false)
    private BillingDetailsMapper billingDetailsMapper ;

    @Override
    public Page<BillingDetailsVO> listByPage(BillingDetailsQueryDTO queryDTO) {
        if (queryDTO == null) {
            queryDTO = new BillingDetailsQueryDTO();
        }
        queryDTO.setOrderBy(FormatUtils.formatOrderBy(queryDTO.getOrderBy()));
        queryDTO.setConsumptionName(FormatUtils.makeFuzzySearchTerm(queryDTO.getConsumptionName()));
        queryDTO.setConsumptionTime(FormatUtils.makeFuzzySearchTerm(queryDTO.getConsumptionTime()));
        Integer size = billingDetailsMapper.count(queryDTO);
        PageUtils pageUtils = new PageUtils(queryDTO.getCurrent(), queryDTO.getPageSize(), size);

        if (size == 0) {
            // 没有命中，则返回空数据。
            return pageUtils.getNullPage();
        }
        // 利用myBatis到数据库中查询数据，以分页的方式
        List<BillingDetails> list = billingDetailsMapper.list(queryDTO, pageUtils.getOffset(), pageUtils.getLimit());
        List<BillingDetailsVO> voList = new ArrayList<>();
        for (BillingDetails billingDetails : list) {
            // 对象转VO对象
            BillingDetailsVO vo = BillingDetailsUtils.convertToVO(billingDetails);
            voList.add(vo);
        }

        return new Page<>(pageUtils.getCurrent(), pageUtils.getPageSize(), pageUtils.getTotal(), voList);
    }
    @Override//修改
    public Select<TimeSelectVO> timeSelect(TimeQueryDTO timeQueryDTO){
        Select<TimeSelectVO> data=new Select<>(new ArrayList<>());
        List<TimeSelectDTO> list=billingDetailsMapper.timeSelect(timeQueryDTO);
        for (TimeSelectDTO timeSelect:list){
            TimeSelectVO timeSelectVO = new TimeSelectVO();
            BeanUtils.copyProperties(timeSelect, timeSelectVO);
            data.getList().add(timeSelectVO);
        }
        return data;
    }
    @Override
    public Select<NameSelectVO> nameSelect(NameQueryDTO nameQueryDTO){
        nameQueryDTO.setConsumptionName(FormatUtils.makeFuzzySearchTerm(nameQueryDTO.getConsumptionName()));
        Select<NameSelectVO> data=new Select<>(new ArrayList<>());
        List<NameSelectDTO> list=billingDetailsMapper.nameSelect(nameQueryDTO);
        for (NameSelectDTO nameSelect :list){
            NameSelectVO nameSelectVO = new NameSelectVO();
            BeanUtils.copyProperties(nameSelect,nameSelectVO);
            data.getList().add(nameSelectVO);
        }
        return data;
    }

    @Override//新建账目
    public Integer insert(BillingDetailsDTO billingDetailsDTO) {
        BillingDetailsUtils.validateBillingDetails(billingDetailsDTO);
        BillingDetails billingDetails = new BillingDetails();
        BeanUtils.copyProperties(billingDetailsDTO, billingDetails);
        billingDetails.setConsumptionTime(billingDetailsDTO.getConsumptionTime());
        billingDetails.setModificationTime(billingDetailsDTO.getModificationTime());
        billingDetailsMapper.insert(billingDetails);
        return billingDetails.getId();
    }
    public Integer updateBillingDetails(BillingDetailsDTO billingDetailsDTO) {
        // 校验输入数据正确性
        Token token = ThreadContextHolder.getToken();
        BillingDetailsUtils.validateBillingDetails(billingDetailsDTO);
        Assert.notNull(billingDetailsDTO.getId(), "id不能为空");
        BillingDetails billingDetails = billingDetailsMapper.selectByPrimaryKey(billingDetailsDTO.getId());
        Assert.notNull(billingDetails, "没有找到消费记录，Id为：" + billingDetailsDTO.getId());

        BeanUtils.copyProperties(billingDetailsDTO, billingDetails);
        billingDetailsMapper.updateByPrimaryKey(billingDetails);
        return billingDetails.getId();
    }
    @Override
    public void deleteByCodes(List<Integer> ids) {
        Assert.notEmpty(ids, "部门id列表不能为空");
        billingDetailsMapper.deleteByCodes(ids);
    }
}
